set seed 21112023

***THIS CODE WILL GENERATE BOOTSTRAP SAMPLES, WHICH WE SAVE, THEN ESTIMATE IN A LATER DO FILE
**ALLOWS US TO RUN ESTIMATION ON MULTIPLE COMPUTERS (WHILE FIXING SEED)
local bsamplesize = 100

********************FULL SAMPLE

use "$working/Demand_TFPQIV_Qklme_sampleFS", clear
merge 1:1 co_code1 year using "$working/ProdQ_new_klme_sampleSS_allobs"

generate mid_s = 1 if _merge==3 // D and SS
replace mid_s = 2 if _merge==1 // D only
replace mid_s = 3 if _merge==2 // SS only

drop _merge

merge  1:1 co_code1 year using "$working/ProdQ_new_klme_sampleFS"

generate samples = 1 if _merge==3 & mid_s ==1 // D, FS, SS
replace samples = 2 if _merge==3 & mid_s==2 // D and FS
replace samples = 3 if _merge==3 & mid_s==3 // SS and FS

replace samples = 4 if _merge==1 & mid_s==1 // D and SS only
replace samples  = 5 if _merge==1 & mid_s==2 // D only
replace samples  = 6 if _merge==1 & mid_s==3 // SS and FS

replace samples = 7 if _merge==2 // FS only

tabulate samples

**grab the products that are not in D 
preserve
	drop _merge
	merge 1:m co_code1 year using "$working/ProdQ_new_klme_sampleFS_prod"
	keep if _merge==3 & samples==3 | samples==7
	drop _merge
	sum year
	tempfile t1
	save `t1', replace
restore

*Grab the SS firm obs (only in SS)
preserve
	drop _merge
	keep if samples==6
	sum year
	tempfile t2
	save `t2', replace
restore
tabulate samples

drop _merge
drop mid_s

merge 1:m co_code1 year using "$working/Demand_TFPQIV_Qklme_sampleFS_prod"
drop if _merge==1 // These are products that are only in FS, not demand
drop _merge
append using `t1'
append using `t2'

replace samples = 1 if samples==2 // 1 is (D,FS,SS) + (D,FS)
replace samples = 1 if samples==4 // 1 is (D,FS,SS) + (D,FS) + (D,SS)
replace samples = 1 if samples==7 // 1 is (D,FS,SS) + (D,FS) + (D,SS) + (FS) 


preserve

	collapse (mean) samples, by(co_code1 year)

	sum

restore

tabulate samples
	
save "$working/sample_ProdQ_TFPQIV", replace

forvalues bs = 1(1)`bsamplesize' {
	clear
	***Grab bootstrap samples
	use "$working/sample_ProdQ_TFPQIV"
	bsample,  strata(samples) cluster(co_code1) idcluster(bs_co_code1)
	save "$BS_fold_old/record/Qklme/allobs/bssample_`bs'", replace
}



**************Decomposition Sample

use "$working/Demand_TFPQIV_Qklme_sampleFS", clear
merge 1:1 co_code1 year using "$working/ProdQ_new_klme_sampleSS"

generate mid_s = 1 if _merge==3 // D and SS
replace mid_s = 2 if _merge==1 // D only
replace mid_s = 3 if _merge==2 // SS only

drop _merge

merge  1:1 co_code1 year using "$working/ProdQ_new_klme_sampleFS"

generate samples = 1 if _merge==3 & mid_s ==1 // D, FS, SS
replace samples = 2 if _merge==3 & mid_s==2 // D and FS
replace samples = 3 if _merge==3 & mid_s==3 // SS and FS

replace samples = 4 if _merge==1 & mid_s==1 // D and SS only
replace samples  = 5 if _merge==1 & mid_s==2 // D only
replace samples  = 6 if _merge==1 & mid_s==3 // SS and FS

replace samples = 7 if _merge==2 // FS only

tabulate samples

**Samples 2 is too rare: put into 1 
replace samples = 1 if samples==2
drop _merge
drop mid_s

sum
**grab the products that are just in FS
preserve
	merge 1:m co_code1 year using "$working/ProdQ_new_klme_sampleFS_prod"
	keep if _merge==3 & samples==3 | samples==7
	drop _merge
	tempfile t1
	save `t1', replace
restore

merge 1:m co_code1 year using "$working/Demand_TFPQIV_Qklme_sampleFS_prod"
drop if _merge==1 // These are products that are only in FS, not demand
drop _merge
append using `t1'

preserve

	collapse (mean) samples, by(co_code1 year)

	sum

restore
	
save "$working/sample_ProdQ_TFPQIV", replace

forvalues bs = 1(1)`bsamplesize' {
	clear
	***Grab bootstrap samples
	use "$working/sample_ProdQ_TFPQIV"
	bsample,  strata(samples) cluster(co_code1) idcluster(bs_co_code1)
	save "$BS_fold_old/record/Qklme/base/bssample_`bs'", replace
}
